package service

import (
	"context"
	"order-srv/basic/global"
	__ "order-srv/basic/proto"
	"order-srv/internal/model"
)

type Service struct {
	__.UnimplementedOrderServer
}

// 用户添加
func (s *Service) AddUser(_ context.Context, req *__.AddUserRequest) (resp *__.AddUserResponse, err error) {
	user := model.User{
		Username: req.Username,
		Password: req.Password, // ⚠️ 实际开发中要加密
		Email:    req.Email,
		Phone:    req.Phone,
		Status:   int(req.Status),
		VipLevel: int(req.VipLevel),
	}

	if err = global.DB.Create(&user).Error; err != nil {
		return nil, err
	}

	return &__.AddUserResponse{
		UserId:  user.UserID,
		Message: "添加用户成功",
	}, nil
}

// 商品添加
func (s *Service) AddProduct(_ context.Context, req *__.AddProductRequest) (resp *__.AddProductResponse, err error) {
	product := model.Product{
		ProductName: req.ProductName,
		CategoryID:  req.CategoryId,
		BrandID:     req.BrandId,
		Price:       req.Price,
		Stock:       int(req.Stock),
		Description: req.Description,
		Status:      int(req.Status),
		ImageURL:    req.ImageUrl,
	}

	if err = global.DB.Create(&product).Error; err != nil {
		return nil, err
	}

	return &__.AddProductResponse{
		ProductId: product.ProductID,
		Message:   "添加商品成功",
	}, nil
}

// 订单添加
func (s *Service) AddOrder(_ context.Context, req *__.AddOrderRequest) (resp *__.AddOrderResponse, err error) {

	order := model.Order{
		UserID:      req.UserId,
		OrderNo:     req.OrderNo,
		TotalAmount: req.TotalAmount,
		PayAmount:   req.PayAmount,
		OrderStatus: int(req.OrderStatus),
		PayType:     int(req.PayType),
		AddressID:   req.AddressId,
		OrderType:   int(req.OrderType),
	}

	if err = global.DB.Create(&order).Error; err != nil {
		return nil, err
	}

	return &__.AddOrderResponse{
		OrderId: order.OrderID,
		Message: "添加订单成功",
	}, nil
}

// 订单详情添加
func (s *Service) AddOrderItem(_ context.Context, req *__.AddOrderItemRequest) (resp *__.AddOrderItemResponse, err error) {

	total := req.ProductPrice * float64(req.Quantity)

	item := model.OrderItem{
		OrderID:      req.OrderId,
		ProductID:    req.ProductId,
		ProductName:  req.ProductName,
		ProductPrice: req.ProductPrice,
		Quantity:     int(req.Quantity),
		TotalPrice:   total,
		Discount:     req.Discount,
	}

	if err = global.DB.Create(&item).Error; err != nil {
		return nil, err
	}

	return &__.AddOrderItemResponse{
		ItemId:  item.ItemID,
		Message: "添加订单项成功",
	}, nil
}

// 分类添加
func (s *Service) AddCategory(_ context.Context, req *__.AddCategoryRequest) (resp *__.AddCategoryResponse, err error) {
	category := model.Category{
		CategoryName: req.CategoryName,
		ParentID:     req.ParentId,
		Level:        int(req.Level),
		SortOrder:    int(req.SortOrder),
		Status:       int(req.Status),
		IconURL:      req.IconUrl,
	}

	if err = global.DB.Create(&category).Error; err != nil {
		return nil, err
	}

	return &__.AddCategoryResponse{
		CategoryId: category.CategoryID,
		Message:    "添加分类成功",
	}, nil
}

// 品牌添加
func (s *Service) AddBrand(_ context.Context, req *__.AddBrandRequest) (resp *__.AddBrandResponse, err error) {
	brand := model.Brand{
		BrandName:   req.BrandName,
		Description: req.Description,
		LogoURL:     req.LogoUrl,
		Status:      int(req.Status),
	}

	if err = global.DB.Create(&brand).Error; err != nil {
		return nil, err
	}

	return &__.AddBrandResponse{
		BrandId: brand.BrandID,
		Message: "添加品牌成功",
	}, nil
}
